﻿# ---------------------- #
# Subscription Functions #
# ---------------------- #

# Execute "Add" operation
function Process-SubscriptionsAdd
{
    foreach ($subscription in $xml.configuration.subscriptions.subscription)
    {
	    if (!(Should-Run($subscription))) {continue}        
        Write-Information "Subscription '$($subscription.name)' processing started."
        Select-Subscription $subscription
        Add-ResourceGroups $subscription
	    Add-VirtualNetworks $subscription
	    Add-StorageAccounts $subscription
	    Add-ReservedIPs $subscription
        Add-NetworkSecurityGroups $subscription
	    Add-VirtualMachines $subscription
        Write-Information "Subscription '$($subscription.name)' processing completed."
    }
}

# Execute "Remove" operation
function Process-SubscriptionsRemove
{
    foreach ($subscription in $xml.configuration.subscriptions.subscription)
    {
	    if (!(Should-Run($subscription))) {continue}        
        Write-Information "Subscription '$($subscription.name)' processing started."
        Select-Subscription $subscription
	    Remove-VirtualMachines $subscription
        Remove-NetworkSecurityGroups $subscription
        Remove-ReservedIPs $subscription
	    Remove-StorageAccounts $subscription
	    Remove-VirtualNetworks $subscription
        Remove-ResourceGroups $subscription
        Write-Information "Subscription '$($subscription.name)' processing completed."
    }
}

# Execute "Start" operation
function Process-SubscriptionsStart
{
    foreach ($subscription in $xml.configuration.subscriptions.subscription)
    {
	    if (!(Should-Run($subscription))) {continue}        
        Write-Information "Subscription '$($subscription.name)' processing started."
        Select-Subscription $subscription
	    Start-VirtualMachines $subscription
        Write-Information "Subscription '$($subscription.name)' processing completed."
    }
}

# Execute "Stop" operation
function Process-SubscriptionsStop
{
    foreach ($subscription in $xml.configuration.subscriptions.subscription)
    {
	    if (!(Should-Run($subscription))) {continue}        
        Write-Information "Subscription '$($subscription.name)' processing started."
        Select-Subscription $subscription
	    Stop-VirtualMachines $subscription
        Write-Information "Subscription '$($subscription.name)' processing completed."
    }
}

# Execute "SnapshotCreate" operation
function Process-SubscriptionsSnapshotCreate()
{
    foreach ($subscription in $xml.configuration.subscriptions.subscription)
    {
	    if (!(Should-Run($subscription))) {continue}        
        Write-Information "Subscription '$($subscription.name)' processing started."
        Select-Subscription $subscription
	    SnapshotCreate-VirtualMachines $subscription
        Write-Information "Subscription '$($subscription.name)' processing completed."
    }
}

# Execute "SnapshotList" operation
function Process-SubscriptionsSnapshotList()
{
    foreach ($subscription in $xml.configuration.subscriptions.subscription)
    {
	    if (!(Should-Run($subscription))) {continue}        
        Write-Information "Subscription '$($subscription.name)' processing started."
        Select-Subscription $subscription
	    SnapshotList-VirtualMachines $subscription
        Write-Information "Subscription '$($subscription.name)' processing completed."
    }
}

# Execute "SnapshotRemove" operation
function Process-SubscriptionsSnapshotRemove()
{
    foreach ($subscription in $xml.configuration.subscriptions.subscription)
    {
	    if (!(Should-Run($subscription))) {continue}        
        Write-Information "Subscription '$($subscription.name)' processing started."
        Select-Subscription $subscription
	    SnapshotRemove-VirtualMachines $subscription
        Write-Information "Subscription '$($subscription.name)' processing completed."
    }
}

# Execute "SnapshotRestore" operation
function Process-SubscriptionsSnapshotRestore()
{
    foreach ($subscription in $xml.configuration.subscriptions.subscription)
    {
	    if (!(Should-Run($subscription))) {continue}        
        Write-Information "Subscription '$($subscription.name)' processing started."
        Select-Subscription $subscription
	    SnapshotRestore-VirtualMachines $subscription
        Write-Information "Subscription '$($subscription.name)' processing completed."
    }
}

# Execute "DefinitionExport" operation
function Process-SubscriptionsDefinitionExport()
{
    foreach ($subscription in $xml.configuration.subscriptions.subscription)
    {
	    if (!(Should-Run($subscription))) {continue}        
        Write-Information "Subscription '$($subscription.name)' processing started."
        Select-Subscription $subscription
	    DefinitionExport-VirtualMachines $subscription
        Write-Information "Subscription '$($subscription.name)' processing completed."
    }
}

# Execute "DefinitionRemove" operation
function Process-SubscriptionsDefinitionRemove()
{
    foreach ($subscription in $xml.configuration.subscriptions.subscription)
    {
	    if (!(Should-Run($subscription))) {continue}        
        Write-Information "Subscription '$($subscription.name)' processing started."
        Select-Subscription $subscription
	    DefinitionRemove-VirtualMachines $subscription
        Write-Information "Subscription '$($subscription.name)' processing completed."
    }
}

# Execute "DefinitionImport" operation
function Process-SubscriptionsDefinitionImport()
{
    foreach ($subscription in $xml.configuration.subscriptions.subscription)
    {
	    if (!(Should-Run($subscription))) {continue}        
        Write-Information "Subscription '$($subscription.name)' processing started."
        Select-Subscription $subscription
	    DefinitionImport-VirtualMachines $subscription
        Write-Information "Subscription '$($subscription.name)' processing completed."
    }
}

# Select current subscription
function Select-Subscription([System.Xml.XmlElement]$subscription)
{
    if ($subscription.adminUserName -ne $null -and $subscription.password -ne $null)
    {
        if ((Get-AzureAccount | ? { $_.Type -eq "User" -and $_.Id -eq $subscription.adminUserName }) -eq $null)
        {
            $credential = New-Object System.Management.Automation.PSCredential($subscription.adminUserName, (ConvertTo-SecureString $subscription.password -AsPlainText -Force))
            Add-AzureAccount -Credential $credential | Out-Null
        }
    }
    if ((Get-AzureSubscription -SubscriptionName $subscription.name) -eq $null)
    {
        throw "Subscription '$($subscription.name)' is not found."
    }
	Select-AzureSubscription -SubscriptionName $subscription.name -Current
}